package com.epam.action;

import com.epam.dao.h2.H2StudentDao;
import com.epam.dao.h2.H2TeacherDao;
import com.epam.dao.h2.H2UserDao;
import com.epam.entity.Student;
import com.epam.entity.Teacher;
import com.epam.entity.User;
import com.epam.service.TrainingListForTeacher;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginAction implements Action {
	private static Logger LOGGER = LoggerFactory.getLogger(LoginAction.class);
	private static H2UserDao h2UserDao = new H2UserDao();
	private static H2StudentDao h2StudentDao = new H2StudentDao();
	private static H2TeacherDao h2TeacherDao = new H2TeacherDao();

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {
		String username = null;
		String password = null;		
		String checkType = request.getParameter("save");
		if (checkType != null) {
			username = request.getParameter("username");
			password = request.getParameter("password");
		} else {
			if (checkCookie(request, "username")) {
				if (checkCookie(request, "password")) {					
					username = getUserCookie(request, "username");
					password = getUserCookie(request, "password");					
				}
			} else {
				username = request.getParameter("username");
				password = request.getParameter("password");
			}
		}
		try {
			User user = h2UserDao.selectByName(username, password);
			if (user != null) {
				if (checkType !=null) {
					response.addCookie(setUserCookie("username", username));
					response.addCookie(setUserCookie("password", password));
				}
				if (user.getRole().toString().equals("STUDENT")) {
					Student student = h2StudentDao.selectByUserId(user);
					request.getSession().setAttribute("student", student);
					return "/studentscabinet.jsp";
				}
				if (user.getRole().toString().equals("TEACHER")) {
					Teacher teacher = h2TeacherDao.selectByUserId(user);
					request.getSession().setAttribute("teacher", teacher);
					TrainingListForTeacher trList = new TrainingListForTeacher(
							teacher.getId());
					request.setAttribute("trlist", trList);
					return "/teacherscabinet.jsp";
				}
			} else {
				request.getSession().setAttribute("errorMessage",
						"Invailid username/password");
				request.getSession().setAttribute("initialpage",
						"/index.jsp");				
			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}

		return "/errormessage.jsp";
	}

	private Cookie setUserCookie(String cookieName, String cookieValue) {
		Cookie cookie = new Cookie(cookieName, cookieValue);
		cookie.setMaxAge(60 * 24 * 60 * 60);
		return cookie;
	}

	private boolean checkCookie(HttpServletRequest request,
			 String cookieName) {
		boolean flag=false;
		Cookie[] cookies = request.getCookies();	
		if (cookies != null) {
			for (Cookie ck : cookies) {
				if (ck.getName().equals(cookieName)) {
					flag=true;
					break;
				}
			}
		}
		return flag;
	}
	
	private String getUserCookie(HttpServletRequest request,
			 String cookieName){
		String cookieValue= null;
		Cookie[] cookies = request.getCookies();
		if (cookies!= null){
			for(Cookie ck:cookies){
				if(ck.getName().equals(cookieName)){
					cookieValue = ck.getValue();
					break;
				}
			}
		}
		return cookieValue;
	}
}
